package com.health.tools;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Build;
import android.os.StrictMode;
import android.util.Log;

public class WebServiceHelper {
	static {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			// 4.0以后需要加入下列两行代码，才可以访问Web Service
			StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites()
					.detectNetwork().penaltyLog().build());

			StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects()
					.detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
		}
		// 4.0以前版本不需要以上设置
	}

	/**
	 * @param url
	 *            web service路径
	 * @param nameSpace
	 *            web service名称空间
	 * @param methodName
	 *            web service方法名称
	 * @param params
	 *            web service方法参数
	 * @throws XmlPullParserException 
	 * @throws IOException 
	 */
	public static String getSoapObject(String serviceUrl, String methodName, String nameSpace, String soapAction,
			HashMap<String, Object> params) throws IOException, XmlPullParserException {
		String URL = serviceUrl;
		String NAMESPACE = nameSpace;// 名称空间，服务器端生成的namespace属性值
		String METHOD_NAME = methodName;
		String SOAP_ACTION = soapAction;

		String result = "";
		//try {
		SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
			if (params != null && params.size() > 0) {
				for (Entry<String, Object> item : params.entrySet()) {
					rpc.addProperty(item.getKey(), item.getValue().toString());
				}
			}

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
			envelope.bodyOut = rpc;
			envelope.dotNet = false;// true--net; false--java;
			envelope.setOutputSoapObject(rpc);
			HttpTransportSE ht = new HttpTransportSE(URL);
			ht.debug = true;
			ht.call(null, envelope);
			if (envelope.getResponse() != null) {
				Object object = (Object) envelope.getResponse();
 				result = object.toString();
 				Log.w("dddddwebservice", result);
//				try {
//					soap = (SoapObject) envelope.getResponse();
//					Log.w("dddddddddddddddddddWEbservice", soap.toString());
//				} catch (Exception e) {
//					soap = (SoapObject) envelope.bodyIn;
//				}
			}

		//} catch (Exception ex) {
		//	ex.printStackTrace();
		//}
		return result;
	}
}
